package com.ali.Reggie.controller;

import com.ali.Reggie.common.R;
import com.ali.Reggie.entity.Category;
import com.ali.Reggie.entity.Employee;
import com.ali.Reggie.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ：ALi
 * @date ：Created in 2022/6/9 14:42
 * @description：分类管理类
 * @modified By：
 * @version: $
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class categoryController {

    @Autowired
    private CategoryService categoryService;

    //新增数据
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("新增成功");
    }
    //分页查询
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize){

        Page<Category> page1 = new Page<>(page,pageSize);
        //构建条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort);

        categoryService.page(page1,queryWrapper);

        return R.success(page1);
    }

    //修改方法
    @PutMapping()
    public R<String> update(@RequestBody Category category){
        boolean res = categoryService.updateById(category);
        if(res){
            return R.success("更新成功");
        }else{
            return R.error("更新失败");
        }
    }

    //删除分类
    @DeleteMapping()
    public R<String> deleteById(Long ids){
        categoryService.remove(ids);
        return R.success("删除成功!");
    }

    //菜品新增的下拉框
    @GetMapping("/list")
    public R<List<Category>> addList(Category category){
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        lambdaQueryWrapper.orderByAsc(Category::getSort).orderByAsc(Category::getUpdateTime);

        List<Category> list = categoryService.list(lambdaQueryWrapper);

        return R.success(list);

    }


}